/*
 * A Gneral purpose java based Genome Library created by Susanta Tewari.
 * Copyright is protected. Contact Susanta Tewari at <statsusant@yahoo.com>
 */

package genomemap.barratt;

import javautil.io.IOUtil;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.logging.Logger;

/**
 * The file is expected to have only 2 columns. But having more than 2 columns does not interrupt the
 * process as long as the first data correspond to the physical distance and the second to the
 * recombination distance.
 * @author Susanta Tewari <stewari@yahoo.com>
 */
public class BarrattModelFileDAO implements BarrattModelDAO {

    /** Field description */
    private static final Logger logger = Logger.getLogger(BarrattModelFileDAO.class.getName());

    /**
     * By default, default delimiter set, which is " \t\n\r\f": the space character, the tab character,
     * the newline character, the carriage-return character, and the form-feed character.
     * However you can set a different one via the setter method.
     */
    private String fileDelim = null;

    /** Field description */
    private final String filePath;

    /**
     * Constructs ...
     * @param filePath1
     */
    public BarrattModelFileDAO(String filePath1) {
        filePath = filePath1;
    }

    public BarrattModelData getBarrattModelData() {

        BarrattModelData barrattModelData = new BarrattModelData();

        try {

            try (BufferedReader inFile = new BufferedReader(new FileReader(new File(filePath)))) {

                String line       = "";
                boolean firstTime = true;
                int lineCount     = 0;

                while ((line = inFile.readLine()) != null) {

                    lineCount++;

                    String[] vals = IOUtil.getTokens(line, fileDelim);


                    // we are expecting only two values:  read the first two into double
                    if (vals.length != 2) {

                        if (firstTime) {

                            logger.warning(
                                "Barratt Model data has more than 2 columns ! line number: "
                                + lineCount);

                            firstTime = false;
                        }
                    }

                    barrattModelData.addData(vals[0], vals[1]);
                }
            }

        } catch (IOException ex) {
            logger.severe(ex.getMessage());
        }

        return barrattModelData;
    }
}
